#include <iostream>
#include <thread>
#include <chrono>
#include <atomic>
#include <iomanip>
#include "include/timer_wheel.h"
#include "include/logger.hpp"

using namespace std;
using namespace HAL;

// ====================== 工具函数：打印当前时间 ======================
string now_time()
{
    using namespace std::chrono;
    auto now = system_clock::now();
    auto t = system_clock::to_time_t(now);
    auto ms = duration_cast<milliseconds>(now.time_since_epoch()) % 1000;

    std::stringstream ss;
    ss << put_time(localtime(&t), "%Y-%m-%d %H:%M:%S");
    ss << "." << setfill('0') << setw(3) << ms.count();
    return ss.str();
}

// ====================== 主测试程序 ======================
int main()
{
    cout << "==================== 定时器测试开始 ====================" << endl;
    Logger::Info("这是一条 INFO 日志");

    // 创建 TimerWheel
    TimerWheel timer(10);

    cout << "[测试] 启动定时器..." << endl;
    timer.Start();

    //----------------------------------------------------------
    // 1. 基础延时任务
    //----------------------------------------------------------
    TimerEvent ev1;
    ev1.id = 1;
    ev1.name = "基础延时任务";
    ev1.callback = []() {
        cout << "[回调 " << now_time() << "] 基础延时任务执行！（100ms 后）" << endl;
    };

    timer.AddTask(100, ev1);

    //----------------------------------------------------------
    // 2. 多任务并发测试
    //----------------------------------------------------------
    for (int i = 2; i <= 5; i++) {
        TimerEvent ev;
        ev.id = i;
        ev.name = "任务_" + to_string(i);
        ev.callback = [i]() {
            cout << "[回调 " << now_time() << "] 任务 " << i << " 执行！" << endl;
        };
        timer.AddTask(i * 120, ev);
    }

    //----------------------------------------------------------
    // 3. 重复任务
    //----------------------------------------------------------
    TimerEvent evRepeat;
    evRepeat.id = 10;
    evRepeat.name = "重复任务";
    evRepeat.setRepeat(true);
    evRepeat.callback = []() {
        cout << "[回调 " << now_time() << "] 重复任务执行一次！（每 200ms）" << endl;
    };

    timer.AddTask(200, evRepeat);

    //----------------------------------------------------------
    // 4. 取消任务
    //----------------------------------------------------------
    TimerEvent evCancel;
    evCancel.id = 20;
    evCancel.name = "取消测试任务";
    evCancel.callback = []() {
        cout << "[回调 " << now_time() << "] 这个任务不应该执行！！！" << endl;
    };
    timer.AddTask(300, evCancel);

    cout << "[测试] 150ms 后取消任务 20..." << endl;
    std::this_thread::sleep_for(std::chrono::milliseconds(150));
    timer.CancelTask(20);

    //----------------------------------------------------------
    // 5. 大延时任务（2 秒）
    //----------------------------------------------------------
    TimerEvent evLong;
    evLong.id = 30;
    evLong.name = "大延时任务";
    evLong.callback = []() {
        cout << "[回调 " << now_time() << "] 大延时任务执行！（2000ms）" << endl;
    };
    timer.AddTask(2000, evLong);

    //----------------------------------------------------------
    // 6. Stop 测试
    //----------------------------------------------------------
    cout << "[测试] 500ms 后暂停定时器 1 秒" << endl;
    std::this_thread::sleep_for(std::chrono::milliseconds(500));
    timer.Stop();
    std::cout << "定时器停止时间 " << now_time() << std::endl;

    cout << "[测试] TimerWheel 已暂停，提交一个暂停期间任务（不应执行）" << endl;

    TimerEvent evPause;
    evPause.id = 40;
    evPause.name = "暂停期间任务";
    evPause.callback = []() {
        cout << "[回调 " << now_time() << "] 暂停期间的任务不应该执行！（BUG 提示）" << endl;
    };
    timer.AddTask(300, evPause);

    std::this_thread::sleep_for(std::chrono::seconds(1));
    cout << "[测试] 重新启动定时器" << endl;
    timer.Start();
    
    timer.CancelTask(10);
    std::cout << "任务 10 已取消" << std::endl;

    //----------------------------------------------------------
    // 7. 重复任务运行测试
    //----------------------------------------------------------
    cout << "[测试] 让重复任务执行 2 秒..." << endl;
    std::this_thread::sleep_for(std::chrono::seconds(2));

    //----------------------------------------------------------
    // 8. 停止测试
    //----------------------------------------------------------
    cout << "[测试] 停止定时器..." << endl;
    timer.Stop();

    cout << "==================== 定时器测试结束 ====================" << endl;
    return 0;
}
